package com.shuaishuai.ketaiheadlines.biz;

import com.shuaishuai.ketaiheadlines.dao.NewsMapper;
import com.shuaishuai.ketaiheadlines.dao.NewscategoriesMapper;
import com.shuaishuai.ketaiheadlines.pojo.News;
import com.shuaishuai.ketaiheadlines.pojo.Newscategories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Newscategoriesbiz {

    @Autowired(required = false)
    private NewsMapper newsMapper;
    @Autowired(required = false)
    private NewscategoriesMapper newscategoriesMapper;

    public List<Newscategories> getListNewscategories(String name){
        return newscategoriesMapper.selectListNewscategories(name);
    }

    public int deleteNewscategories(Integer categoryId){
        try {
            List<News> newsList = newsMapper.selectAllById(categoryId);
            for (News news : newsList) {
                newsMapper.deleteByPrimaryKey(news.getNewsId());
            }
            newscategoriesMapper.deleteByPrimaryKey(categoryId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public int insertNewscategories(String name) {
       return newscategoriesMapper.insertNewscategories(name);
    }

    public int updateNewscategories(Integer categoryId, String name) {
       return newscategoriesMapper.updateByPrimaryKeySelective(new Newscategories(categoryId,name));
    }
}
